﻿@using System.Configuration
@using System.Data.SqlClient
@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Sync B2E Page";

    bool hasError = false;
    string errorMessage = string.Empty;
    bool hasSuccess = false;
    string successMessage = string.Empty;

    System.Data.DataSet ds = new System.Data.DataSet("CurrentClaims");
    System.Data.DataTable dtB2E = null;
    
    
    if (IsPost)
    {
        if (Request.Form["actionButton"] == "update")
        {
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=kldb;Initial Catalog=B2EPilot;User ID=b2epilotwebacct;Password=p@55w0rd;Application Name=Staff-Overtime;Workstation ID=AN2594"))
            {
                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText =
@"SELECT  e.EmployeeMaster, b.Intranet_ID, p.NameLegal, a.Section, e.AccessLevel, NULLIF(RTRIM(LTRIM(p.Internet)), '') AS 'Email'
FROM    tblDataB2E_SSO b 
INNER JOIN 
        tblDataPersonnelB2E e 
        ON b.Intranet_ID = e.LoginID 
INNER JOIN 
        tblDataPersonnel p 
        ON e.EmployeeMaster = p.EmployeeMaster
INNER JOIN 
        tblDataActivation a 
        ON p.EmployeeMaster = a.EmployeeMaster
        AND p.DateOfTermination IS NULL";

                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                da.Fill(ds, "B2E");

                if ((ds != null) && (ds.Tables.Count > 0) && (ds.Tables["B2E"].Rows.Count > 0))
                {
                    dtB2E = ds.Tables["B2E"];
                }
            }

            if (dtB2E != null)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EOCS"].ConnectionString))
                {
                    System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();

                    conn.Open();
                    using (System.Data.SqlClient.SqlTransaction sqlTran = conn.BeginTransaction())
                    {
                        cmd.CommandText = "DELETE FROM B2E";
                        cmd.Transaction = sqlTran;
                        cmd.ExecuteNonQuery();
                        
                        using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(conn, System.Data.SqlClient.SqlBulkCopyOptions.Default, sqlTran))
                        {
                            bcp.DestinationTableName = "B2E";
                            bcp.WriteToServer(dtB2E);

                            hasSuccess = true;
                            successMessage = @"<span class=""glyphicon glyphicon-ok"" aria-hidden=""true""></span> Sync complete.";
                        }
                        
                        sqlTran.Commit();
                    }
                }
            }
            
        } // if (Request.Form["actionButton"] == "submit")
            
    } // if (IsPost)
    
}

<div class="container-fluid main-content" role="main">

    <h2>
        Sync B2E
    </h2>
    
    <div class="panel panel-default">
        <div class="panel-heading">Instructions (How should this page work?)</div>
        <div class="panel-body">
            <p>
                This page is for users to sync B2E intranet ID and full name.<br />
            </p>

            <ul>
                <li>HR can click on Sync B2E button update records.</li>
            </ul>
        </div>
    </div>

    @if (hasError)
    {
    <div class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Warning!</strong> @errorMessage
    </div>
    }
    @if (hasSuccess)
    {
    <div class="alert alert-success alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <strong>Success!</strong> @Html.Raw(successMessage)
    </div>
    }

    <form method="post">

        <button type="submit" name="actionButton" value="update" class="btn btn-primary">
            <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
            Sync B2E
        </button>

        @Html.Raw(successMessage)
        
    </form>

</div>

@section Scripts
{
    <script type="text/javascript" src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
    <script type="text/javascript" src="~/Scripts/moment.js"></script>
    <script type="text/javascript" src="~/Scripts/moment-with-locales.js"></script>
    <script type="text/javascript">
    (function ($) {
        "use strict";

        var optionsDate, optionsTime, optionsNumeric, moneyNumeric, 
            ctlClaimDate,
            ctlClaimStartWorkTime,
            ctlClaimEndWorkTime,
            ctlClaimStartOT,
            ctlClaimEndOT,
            ctlClaimHours_OT1_5,
            ctlClaimHours_OT2_0,
            ctlClaimHours_PH,
            ctlClaimHours_Less4H,
            ctlClaimHours_More4H,
            ctlClaimAllowance_Shift,
            ctlClaimAllowance_Meal,
            ERROR_STATUS_COLOR,
            NORMAL_STATUS_COLOR;

        ERROR_STATUS_COLOR = "#ff0000";
        NORMAL_STATUS_COLOR = "#ffffff";

        optionsDate = {
            format: "dd/MM/yyyy",
            parseFormats: [
            "dd/MMMM/yyyy",
            "dd-MMMMyyyy",
            "dd/MM/yyyy",
            "dd-MM-yyyy",
            "ddMMyyyy",
            "ddMMyy",
            "ddMM"
            ],
            change: function () {
                if (this.element.val().trim() === "") {
                    this.element.css("background-color", ERROR_STATUS_COLOR);
                    return;
                }

                var value = this.value();

                if (value === null) {
                    this.element.css("background-color", ERROR_STATUS_COLOR);
                    return;
                }

                this.element.css("background-color", NORMAL_STATUS_COLOR);

                console.log(value); //value is the selected date in the numerictextbox
            }
        }; // optionsDate = {

        optionsTime = {
            interval: 15,
            parseFormats: [
            "h:mm tt",
            "h:mmtt",
            "hmmtt",
            "HH:mm",
            "HHmm",
            "htt",
            "HH"
            ], //format also will be added to parseFormats
            change: function () {
                if (this.element.val().trim() === "") {
                    this.element.css("background-color", ERROR_STATUS_COLOR);
                    return;
                }

                var value = this.value();

                if (value === null) {
                    this.element.css("background-color", ERROR_STATUS_COLOR);
                    return;
                }

                this.element.css("background-color", NORMAL_STATUS_COLOR);

                console.log(value); //value is the selected date in the numerictextbox
            }
        }; // optionsTime = {

        optionsNumeric = {
            spinners: false,
            format: "0.##",
            min: 0,
            decimals: 2,
            change: function () {
                var value = this.value();

                console.log(value); //value is the selected date in the numerictextbox
            }
        }; // optionsNumeric = {

        moneyNumeric = {
            spinners: false,
            format: "0.00",
            min: 0,
            decimals: 2,
            change: function () {
                var value = this.value();

                console.log(value); //value is the selected date in the numerictextbox
            }
        }; // moneyNumeric = {

        $(document).ready(function () {

            ctlClaimDate = $("#claimDate").kendoDatePicker(optionsDate);

            ctlClaimStartWorkTime = $("#claimStartWorkTime").kendoTimePicker(optionsTime);
            ctlClaimEndWorkTime = $("#claimEndWorkTime").kendoTimePicker(optionsTime);
            ctlClaimStartOT = $("#claimStartOT").kendoTimePicker(optionsTime);
            ctlClaimEndOT = $("#claimEndOT").kendoTimePicker(optionsTime);

            ctlClaimHours_OT1_5 = $("#claimHours_OT1_5").kendoNumericTextBox(optionsNumeric);
            ctlClaimHours_OT2_0 = $("#claimHours_OT2_0").kendoNumericTextBox(optionsNumeric);
            ctlClaimHours_PH = $("#claimHours_PH").kendoNumericTextBox(optionsNumeric);
            ctlClaimHours_Less4H = $("#claimHours_Less4H").kendoNumericTextBox(optionsNumeric);
            ctlClaimHours_More4H = $("#claimHours_More4H").kendoNumericTextBox(optionsNumeric);

            ctlClaimAllowance_Shift = $("#claimAllowance_Shift").kendoNumericTextBox(moneyNumeric);;
            ctlClaimAllowance_Meal = $("#claimAllowance_Meal").kendoNumericTextBox(moneyNumeric);;

            ////////////////////////////////////////////////////////////////////////////////
            // datepicker
            $(".datepicker").change(function (e) {
                //$(e.target).data("kendoDatePicker").trigger("change");
            });

            ////////////////////////////////////////////////////////////////////////////////
            // timepicker
            $(".timepicker").change(function (e) {
                //$(e.target).data("kendoTimePicker").trigger("change");
            });

        });

    }(jQuery));

    </script>
}